package com.xbongbong.paas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.XbbConstant;
import com.xbongbong.saas.constant.BasicConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DateDiffTypeEnum
 * @Description 日期相减后的单位（用于图表中心的计算字段）
 * @Author mengna.Deng
 * @Date 2021/8/4 10:04
 * @Version 1.0
 */
public enum DateDiffTypeEnum {
    /**
     * 年月日
     */
    YEAR("y", "enum.dateDiffType.name.year","year"),
    MONTH("M", "enum.dateDiffType.name.month","month"),
    DAY("d", "enum.dateDiffType.name.day","day"),
    HOUR("h", "enum.dateDiffType.name.hour","hour"),
    MINUTE("m", "enum.dateDiffType.name.minute","minute"),
    SECOND("s", "enum.dateDiffType.name.second","second"),

    /**
     * 未知规则,当异常时用于返回
     */
    UNKNOWN("",""),
    ;
    private String alias;
    private String name;
    /**
     * 对应的CK sql数学符号
     */
    private String arithmetic;

    DateDiffTypeEnum(String alias, String name) {
        this.alias = alias;
        this.name = name;
    }

    DateDiffTypeEnum(String alias, String name, String arithmetic) {
        this.alias = alias;
        this.name = name;
        this.arithmetic = arithmetic;
    }

    /**
     * 匹配数据集使用的函数
     *
     * @param keyWordList
     * @return java.lang.String
     * @author zyy
     * @date 2022/10/27
     * @version v1.0
     */
    public static String matchKeyWord(List<String> keyWordList) {
        String prefixFunction = XbbConstant.DATE_DIFF;

        if(keyWordList.size() < BasicConstant.THREE){
            return prefixFunction;
        }

        String keyWord = keyWordList.get(BasicConstant.TWO).replaceAll("\"", "");
        DateDiffTypeEnum typeEnum = getByAlias(keyWord);
        switch (typeEnum){
            case YEAR:
                prefixFunction = XbbConstant.YEARS_DIFF;
                break;
            case MONTH:
                prefixFunction = XbbConstant.MONTHS_DIFF;
                break;
            case HOUR:
                prefixFunction = XbbConstant.HOURS_DIFF;
                break;
            case MINUTE:
                prefixFunction = XbbConstant.MINUTES_DIFF;
                break;
            default:
                //默认给DAY ： 1、 用户设置"d"  2、关键词错误  3、用户未设置关键词 （会被上面第一个判断过滤掉）
                break;
        }
        return prefixFunction;
    }

    public String getAlias() {
        return alias;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }

    public String getArithmetic() {
        return arithmetic;
    }

    public static List<DateDiffTypeEnum> getAllEnum() {
        List<DateDiffTypeEnum> list = new ArrayList<>();
        for (DateDiffTypeEnum cache : values()) {
            list.add(cache);
        }
        return list;
    }

    public static DateDiffTypeEnum getByAlias(String alias) {
        for (DateDiffTypeEnum cacheCode : values()) {
            if (cacheCode.getAlias().equals(alias)) {
                return cacheCode;
            }
        }
        return UNKNOWN;
    }

    public static List<String> list() {
        List<String> list = new ArrayList<>();
        for (DateDiffTypeEnum cache : values()) {
            list.add(cache.getAlias());
        }
        return list;
    }

    public static String removeSuffix(String legendContrast){
        for (DateDiffTypeEnum cacheCode : values()) {
            legendContrast = legendContrast.replaceAll("\\("+cacheCode.getName()+"\\)","");
        }
        return legendContrast;
    }
}
